using Senparc.Weixin.Entities;
using Senparc.Weixin.Exceptions;
using System;
namespace Senparc.Weixin.QY.CommonAPIs
{
	public static class AccessTokenHandlerWapper
	{
		public static T Do<T>(string appId, string appSecret, Func<string, T> fun, bool retryIfFaild = true) where T : QyJsonResult
		{
			T result = default(T);
			try
			{
				string arg = AccessTokenContainer.TryGetToken(appId, appSecret, false);
				result = fun(arg);
			}
			catch (ErrorJsonResultException ex)
			{
				if (retryIfFaild && ex.JsonResult.errcode == ReturnCode.获取access_token时AppSecret错误或者access_token无效)
				{
					AccessTokenContainer.TryGetToken(appId, appSecret, true);
					result = AccessTokenHandlerWapper.Do<T>(appId, appSecret, fun, false);
				}
			}
			return result;
		}
	}
}
